<?php
set_time_limit(0);

include_once('lib/config.php');
include_once('lib/Db.class.php');
include_once('lib/Model.class.php');
include_once('lib/Upyun.class.php');
include_once('lib/Image.class.php');
include_once('lib/Http.class.php');
include_once('lib/Cut.class.php');
include_once('lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$mh_copy_hot_ids_queue = 'Queue_zcopy_hot_fetch';
$model = new Model;
$http  = new Http;
$cut   = new Cut;

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||mh_zcopy_hot');

$limit = 200;
while($limit--) {
  $stop = $redis->get('LNPAN_MH_QUEUE_STOP');
  if($stop) {
    die('stop');
  }

  $queue_id = $redis->lPop($mh_copy_hot_ids_queue);
  if(!$queue_id) {
    break;
  }

  $data  = array();
  $where = "state in(0,9) AND id='".$queue_id."' ";

  $info = $model->find('news_zcopy', $where);

  if($info) {
    $url = $info['link'];
    $parse_url = parse_url($url);
    $opt_data = array(
      CURLOPT_REFERER => $url,
      CURLOPT_ENCODING=> 'gzip',
      CURLOPT_HTTPHEADER => array(
        'Referer' => $url,
        'Host' => $parse_url['host'],
        'Pragma' => 'no-cache',
      ),
    );
    $http->opt = $opt_data;
    $responses = $http->curl_http( array($url) );
    $file = $responses[$url]['results'];
    $data = $cut->getData($file, $info);
    $data['http_code'] = $responses[$url]['info']['http_code'].' '.$_SERVER['REMOTE_ADDR'];
    $data['link'] = $url;

    if(empty($data['title2'])) {
      $data['msg'] = '抓取失败请重试';
      if($info['chapter_count'] == 0) {
        $updata = array('state'=>3, 'news_state'=>1);
        $model->save("news_zcopy", "id='$info[id]'", $updata);
      }
    }
    elseif($info['chapter_count'] != $data['chapter_count'] && $data['chapter_count'] > $info['chapter_count']) {
      $data['state'] = $data['status']=='完结' ? 1 : 0;
      $data['chapter_count_old'] = $info['chapter_count'];
      $data['chapter'] = json_encode($data['chapter']);
      $data['chapter_update'] = json_encode($data['chapter_update']);
      $data['info'] = $data['info'] ? $data['info'] : strip_tags($info['info']);
      $data['list_state'] = 0; //有更新
      
      $model->save("news_zcopy", "id='$info[id]'", $data);
      $data['msg'] = '新章节已更新';
    }
    else {
      $data['msg'] = '无更新';
      $data['chapter_count_old'] = $info['chapter_count'];
      $data['chapter'] = json_encode($data['chapter']);
      $data['chapter_update'] = json_encode($data['chapter_update']);
      
      $updata = array();
      $updata['uptime'] = time();
      $updata['state'] = $info['state'];
      if($info['status'] == '完结') {
        $updata['state'] = 1;
      }
      if($info['chapter_count'] == 0) {
        $updata['state'] = 2;
      } elseif(!empty($data['chapter']) && $data['chapter'] != 'null') {
        $updata['chapter'] = $data['chapter'];
        $updata['chapter_update'] = $data['chapter_update'];

        if($info['list_state'] == 1 && $info['news_state'] == 9) {
          $updata['news_state'] = 1;
        }
      }
      if($updata['state'] == 9) {
        $updata['state'] = 0;
      }

      $model->save("news_zcopy", "id='$info[id]'", $updata);
    }
  }

  //print_r($data);
  if($data['msg'] == '新章节已更新') {
    $data['new_date'] = date('Y-m-d H:i:s');
    $redis->lPush('New_zcopy', print_r($data, true));
  }

  //break;
}

$model->close();
$redis->hDel(MH_PIDS, $pid);

